package com.pig4cloud.pig.fx.config;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.shardingsphere.sharding.api.sharding.standard.PreciseShardingValue;
import org.apache.shardingsphere.sharding.api.sharding.standard.RangeShardingValue;
import org.apache.shardingsphere.sharding.api.sharding.standard.StandardShardingAlgorithm;

import java.util.*;

@Slf4j
public class MyStandardShardingAlgorithm implements StandardShardingAlgorithm<Date> {

	// 这个不懂干什么的
	private static final String DATABASES_TABLES_RELEVANCE = "databases-tables-relevance";

	// Getter Setter一定要加，不加拿不到自定义的参数
	// @Getter
	// @Setter
	// private Properties props = new Properties();

	@Override
	public String getType() {
		// 对应yml中的type,这里怎么定义，那里就怎么写 【好像没用上的样子啊 ╮(╯_╰)╭ 】
		return "xiaohang";
	}

	@Override
	public String doSharding(Collection<String> collection, PreciseShardingValue<Date> shardingValue) {
		if (log.isDebugEnabled()) {
			log.debug("=============================>collection: {}, shardingVal: {}", collection, shardingValue);
			log.info("==============================>collection: {}, shardingVal: {}", collection, shardingValue);
		}
		// 这个collection就是节点的集合了 具体的算法就是在这里写的了，具体怎么搞 就看需要了
		for (String each : collection) {
			Date date = shardingValue.getValue();
			if (Objects.isNull(date)) {
				date = new Date();
			}

			String suffix = DateFormatUtils.format(date, "yyyyMM");
			if (each.endsWith(suffix)) {
				return each;
			}
		}
		return null;
	}

	@Override
	public Collection<String> doSharding(Collection<String> collection, RangeShardingValue<Date> shardingValue) {
		if (log.isDebugEnabled()) {
			log.debug("=============================2>collection: {}, shardingVal: {}", collection, shardingValue);
			log.info("==============================2>collection: {}, shardingVal: {}", collection, shardingValue);
		}
		return null;
	}

	@Override
	public Properties getProps() {
		return null;
	}

	@Override
	public void init(Properties properties) {

	}

}
